package com.byteridge.purpleskills;

import java.io.IOException;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class LoginActivity extends Activity {
	String[] credentials = new String[2];
	EditText email_id;
	EditText password;
	Intent puzzelIntent;
	static String URL = "http://purpleskills.com/puzzleservice.svc/LoginUserWithPassword";
	HttpHelper http_helper;
	JSONHelper jsonHelper;
	JSONObject json_object;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		http_helper = new HttpHelper(URL);

		jsonHelper = new JSONHelper();
		json_object = new JSONObject();
		TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
		tabHost.setup();

		TabSpec login_tab = tabHost.newTabSpec("Tab 1");
		login_tab.setContent(R.id.tab1);
		login_tab.setIndicator("Login");

		TabSpec create_user_tab = tabHost.newTabSpec("Tab 2");
		create_user_tab.setIndicator("Create Account");
		create_user_tab.setContent(R.id.tab2);

		TabSpec guest_tab = tabHost.newTabSpec("Tab 3");
		guest_tab.setIndicator("Guest");
		guest_tab.setContent(R.id.tab3);

		tabHost.addTab(login_tab);
		tabHost.addTab(create_user_tab);
		tabHost.addTab(guest_tab);
		 puzzelIntent = new Intent(this, PuzzleActivity.class);
		Button login_button = (Button) findViewById(R.id.sign_in_button);
		email_id = (EditText) findViewById(R.id.email);
		password = (EditText) findViewById(R.id.password);
		login_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				credentials[0] = email_id.getText().toString();
				credentials[1] = password.getText().toString();
				try {
					json_object.put("userEmail", credentials[0]);
					json_object.put("userPassword", credentials[1]);
				} catch (JSONException e) {
					Log.v("PurpleSkills", "JSONException  "+e.getMessage());
				}
				JSONHelper.setJson(json_object);
				Log.v("PurpleSkills", JSONHelper.getJson());
			
				LoginTask login = new LoginTask();
				login.execute();
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
	
	private class LoginTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			String response = "";
			try {
				http_helper.HttpRequest();

			} catch (ClientProtocolException e) {
				Log.v("PurpleSkills", "ClientProtocolException "+e.getMessage());
			} catch (IOException e) {
				Log.v("PurpleSkills", "IOException  "+e.getMessage());
			}
			try {
				response = http_helper.getHttpResponse();
			} catch (ParseException e) {
				Log.v("PurpleSkills", "ParseException  "+e.getMessage());
			} catch (IOException e) {
				Log.v("PurpleSkills", "IOException  "+e.getMessage());
			}
			JSONObject jo = null;
			try {
				jo = new JSONObject(response);
			} catch (JSONException e1) {
				Log.v("PurpleSkills", "JSONException  "+e1.getMessage());
			}
			SharedPreferences pref = getApplicationContext()
					.getSharedPreferences("UserCredentials", 0);
			Editor editor = pref.edit();
			try {
				editor.putString("userEmail", jo.getJSONObject("d").getJSONObject("WSData").getJSONObject("AccessToken").getString("UserEmail"));
				editor.putString("token",
						jo.getJSONObject("d").getJSONObject("WSData")
								.getJSONObject("AccessToken")
								.getString("Token"));
				editor.putString("userName",
						jo.getJSONObject("d").getJSONObject("WSData").getString("UserDisplayName"));
			} catch (JSONException e) {
				Log.v("PurpleSkills", "JSONException  "+e.getMessage());
			}
			editor.commit();
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			startActivity(puzzelIntent);
		}

	}
}
